<?php
/**
 * 这里是文件上传的处理类
 *
 * @file        $Source$
 * 
 * 
 * 
 **/
class Upload
{
    var $allow_type = array();
    var $upload_dir;
    var $upload_file_name;
    var $max_length;
    var $upload_file;
    var $error_code;   //1 --- size too big;2 -- type not allow

    /**
     * 析构函数
     *
     * @param int $max_size  文件上传最大限制
     * @param string $allow_type  文件允许类型，用逗号分开
     */
    function __construct($max_size,$allow_type, $separater='|')
    {
        $this->setMaxLength($max_size);

        if($allow_type)
        {
            $allow_type_array = explode($separater, $allow_type);
            $this->allow_type = $allow_type_array;
        }
    }

    function setAllowType($type)
    {
        $this->allow_type[] = $type;
    }

    function setUploadDir($dir_name)
    {
        $this->upload_dir = $dir_name;
    }

    function setUploadFileName($upload_file_name)
    {
        $this->upload_file_name = $upload_file_name;
    }

    function setMaxLength($max_length)
    {
        $this->max_length = $max_length;
    }

    function isBig($length)
    {
        if($length > $this->max_length)
        {
            $this->error_code = 1;
            return true;
        }
        else
        {
            return false;
        }

    }

    function getFileType($file_name)
    {
        return strtolower(end(explode('.',$file_name)));
    }

    function isAllowType($file_name)
    {
        $fileType = $this->getFileType($file_name);

        if (in_array($fileType,$this->allow_type))
        {
            return true;
        }
        else
        {
            $this->error_code = 2;
            return false;
        }
    }

    function delFile($file_name)
    {
        //echo $file_name;
        return @unlink($file_name) ? true : false;
    }

    function makeDir($directory,$mode = 0777)
    {
        if(@opendir($directory))
        {
            return true;
        }
        else
        {
            if (@mkdir($directory,$mode))
            {
                return true;
            }
            else
            {
                //try to repair the path
                $pathInfo = explode("/",$directory);
                $basedir="";

                foreach($pathInfo as $var)
                {
                    if ($var == ".")
                    {
                        $basedir=$basedir."./";
                        $begin = false;
                    }
                    elseif ($var == "..")
                    {
                        $basedir=$basedir."../";
                        $begin = false;
                    }
                    else
                    {
                        if (!$begin)
                        {
                            $var = $var;
                            $begin = true;
                        }
                        else
                        {
                            $var = '/'.$var;
                        }

                        if ($this->makeDir($basedir.$var,$mode))
                        {
                            //echo "System Auto Repair ${basedir}${var} <br>";
                            $repair = true;
                            $basedir = $basedir.$var;
                        }
                        else
                        {
                            $repair = false;
                        }

                    }

                }

                return $repair;

            }

        }

    }

    /**
     * 增强版的mkdir
     * @param $dir
     * @param $mode
     * @return unknown_type
     */
    function mk_dir($dir, $mode = 0755)
    {
      if (is_dir($dir) || @mkdir($dir,$mode)) return true;
      if (!mk_dir(dirname($dir),$mode)) return false;
      return @mkdir($dir,$mode);
    }


    function uploadFile($files, $is_field=true)
    {
        if($is_field)
        {
            $files    = $_FILES[$files];
        }

        $file_name = $files['name'];
        $file_type = $files['type'];
        $file_temp = $files['tmp_name'];
        $file_size = empty( $length ) ? ($files['size'] + 10) : $length;
        $file_error= $files['error'];

        if( !$this->isAllowType($file_name) || $this->isBig($file_size) || $file_error != 0  )
        {
            return false;
        }
        else
        {
            if(!empty($this->upload_dir) && !empty($this->upload_file_name))
            {
                if ($this->mk_dir($this->upload_dir, 0755))
                {
                    $file_type = $this->getFileType($file_name);//设置文件类别
                    $this->upload_file = $this->upload_file_name . '.' . $file_type;
                    return move_uploaded_file($file_temp , rtrim($this->upload_dir,'/') . '/' . $this->upload_file) ? true : false;
                }
                else return false;
            }
            else return false;
        }
    }
}
?>
